<?php 
namespace Rac\Model;
use Think\Model;
use Think\Page;
class AdminModel extends Model {
	
	public function getAdminList($map) {
		$limit = get_sys_var('page_limit');
		if (empty($limit)) {
			$limit = 10;
		}
		$count      = $this->where($map)->count();
		$Page       = new Page($count, $limit);
		$show       = $Page->show();// 分页显示输出
			
		$field = "*, (SELECT (SELECT auth_group.title FROM auth_group WHERE auth_group.id=auth_group_access.group_id) FROM auth_group_access WHERE auth_group_access.uid=admin.id) AS group_title";
		$data = $this->field($field)->where($map)->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		return array('list'=>$data, 'page'=>$show);
	}
	
	// 更新登录信息
	public function setLoginInfo($id) {
		$map = array('username'=>$username);
		$this->where($map)->setField(
				array('login_time'=>date('Y-m-d H:i:s'),
					  'ip'=>my_get_client_ip()
				)
		);
		$this->where($map)->setInc('login_count');
		
	}
	
	
	public function getAdminInfo($username) {
		$map = array('username'=>$username);
		return $this->where($map)->find();
	}
	
	
	public function add_admin($username, $password, $status, $group_id) {
		//判断用户是否已经添加
		$map = array('username'=>$username);
		$one = $this->where($map)->find();
		if (! empty($one)) {
			return -1;
		}
		
		$data = array('username'=>$username, 
				'password'=>enter_password($password), 
				'status'=>$status,
				'create_time'=>date('Y-m-d H:i:s'),
		);
		$uid = $this->add($data);
		if ($uid > 0) {
			$auth_group_access = array(
				'uid'=>$uid,
				'group_id'=>$group_id
			);
			M('AuthGroupAccess')->add($auth_group_access);
		}
		return $uid;
	}
}


?>